package com.cswnode.demo.service.impl;

import com.cswnode.demo.dao.ClassRoomDao;
import com.cswnode.demo.entity.ClassRoom;
import com.cswnode.demo.enums.YesOrNoEnum;
import com.cswnode.demo.service.ClassRoomService;
import com.cswnode.demo.vo.ClassRoomVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.Date;
import java.util.List;

/**
 * 班级表(ClassRoom)表服务实现类
 *
 * @author makejava
 * @since 2021-01-26 11:05:34
 */
@Service
public class ClassRoomServiceImpl implements ClassRoomService {
    @Autowired
    private ClassRoomDao classRoomDao;

    @Autowired
    private DataSourceTransactionManager transactionManager;

    /**
     * 通过ID查询单条数据
     *
     * @param classId 主键
     * @return 实例对象
     */
    @Override
    public ClassRoom queryById(Long classId) {
        return this.classRoomDao.queryById(classId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ClassRoom> queryAllByLimit(int offset, int limit) {
        return this.classRoomDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<ClassRoomVo> queryVoByLimit(int offset, int limit, String className) {
        return classRoomDao.queryVoByLimit(offset,limit,className);
    }

    /**
     * 新增数据
     *
     * @param classRoom 实例对象
     * @return 实例对象
     */
    @Override
    public ClassRoom insert(ClassRoom classRoom) {
        if (ObjectUtils.isEmpty(classRoom)){
            return null;
        }
        classRoom.setCreateTime(new Date());
        classRoom.setClassId(null);
        classRoom.setIsDelete(YesOrNoEnum.NO.getCode());
        this.classRoomDao.insert(classRoom);
        return classRoom;
    }

    @Override
    public int selectNum(String className) {
        return classRoomDao.queryRows(className);
    }

    /**
     * 修改数据
     *
     * @param classRoom 实例对象
     * @return 实例对象
     */
    @Override
    public ClassRoom update(ClassRoom classRoom) {
        classRoom.setCreateTime(null);
        if (null == classRoom){
            return null;
        }
        this.classRoomDao.update(classRoom);
        return this.queryById(classRoom.getClassId());
    }


    /**
     * 通过主键逻辑删除
     * @param classId 主键
     * @return
     */
    @Override
    @Transactional
    public boolean deleteById(Long classId) {
       if (classId == null){
           return false;
       }
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setName("SomeTxName");
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(def);
        try{
            classRoomDao.deleteById(classId);
            classRoomDao.deleteByStu(classId);
        }catch (Exception e){
            transactionManager.rollback(status);
            throw e;
        }
        return true;
    }
}